﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
/// <summary>
/// Summary description for DangNhap
/// </summary>
public class DangNhap
{
	public DangNhap()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    DataConnection data = new DataConnection();
    public string Dang_Nhap_admim(string ten, string matkhau)
    {
        SqlParameter p1 = new SqlParameter("@ten", ten);
        SqlParameter p2 = new SqlParameter("@mk", matkhau);
        string sql = "select TenDN,MatKhau from NhanVien where TenDN=@ten and MatKhau=@mk";
        DataTable dt = data.GetData(sql, p1,p2);
        if (dt.Rows.Count > 0)
            return dt.Rows[0][0].ToString();
        else
            return "";

    }

    //public DataTable DangKy(string TenDN, string hoten, string matkhau, string gioitinh, string ngaysinh, string diachi, string dienthoai, string email, string CMND)
    //{
    //    string sql = "insert into KhachHang values(TenDN,Hoten,MatKhau,GioiTinh,NgaySinh,DiaChi,DienThoai,Email,CMND) value(@tenDN,@hoten,@matkhau,@gioitinh,@ngaysinh,@diachi,@dienthoai,@Email,@CMND)";
    //    SqlParameter p = new SqlParameter("@tenDN",TenDN);
    //    SqlParameter p1 = new SqlParameter("@hoten", hoten);
    //    SqlParameter p2 = new SqlParameter("@matkhau",matkhau);
    //    SqlParameter p3 = new SqlParameter("@gioitinh", gioitinh);
    //    SqlParameter p4 = new SqlParameter("@ngaysinh", ngaysinh);
    //    SqlParameter p5 = new SqlParameter("@diachi", diachi);
    //    SqlParameter p7 = new SqlParameter("@Email", email);
    //    SqlParameter p6 = new SqlParameter("@dienthoai", dienthoai);
    //    SqlParameter p8 = new SqlParameter("@CMND",CMND);
    //    return data.GetData(sql, p, p1, p2, p3, p4, p5,p7,p6,p8);
    //}
    public bool check(string user)
    {
        SqlParameter p1 = new SqlParameter("@Ten", user);
        DataTable dt = data.GetData("select * from KhachHang where TenDN=user", p1);
        if (dt.Rows.Count > 0)
            return false;
        else
            return true;
    }

//=====================================================================
    public DataTable DSHoiDap()
    {
        string sql = "select * from HoiDap";
        return data.GetData(sql);
    }

    public DataTable ThemCauHoi(string hoten, string email, string chude, string hoi)
    {
        SqlParameter p = new SqlParameter("@NguoiHoi", hoten);
        SqlParameter p1 = new SqlParameter("@Email", email);
        SqlParameter p2 = new SqlParameter("@ChuDe", chude);
        SqlParameter p3 = new SqlParameter("@Hoi",hoi);

        string sql = "insert into HoiDap (NguoiHoi,Email,ChuDe,Hoi) values(@NguoiHoi,@Email,@ChuDe,@Hoi)";
        return data.GetData(sql, p, p1, p2, p3);
    }
    public DataTable TimCauHoi(string NguoiHoi, string ChuDe)
    {
        
        string sql = "select * from HoiDap where 1=1";
        if (NguoiHoi != null)
            sql = sql + " and NguoiHoi like '%'+@NguoiHoi+'%'";
        if (ChuDe != null)
            sql = sql + " and ChuDe like '%'+@chude+'%'";
        SqlParameter p = new SqlParameter("@NguoiHoi", NguoiHoi);
        SqlParameter p1 = new SqlParameter("@chude",ChuDe);
        return data.GetData(sql, p, p1);
    }
    //public DataTable LuuTraLoi(string hoi,string dap)
    //{
    //    string sql="insert into Hoidap values(@Hoi,@Dap)";
    //    SqlParameter phoi = new SqlParameter("@Hoi", hoi);
    //    SqlParameter pdap = new SqlParameter("@Dap", dap);
    //    return data.GetData(sql,phoi,pdap);
    //}
    public DataTable dap()
    {
        string sql = "select Hoi,Dap from HoiDap where HienThi='True'";
        return data.GetData(sql);
    }
    //public DataTable KHHoi()
    //{
    //    string sql = "select Hoi from Hoidap where dap is null";
    //    return data.GetData(sql);
    //}
    public string Dang_Nhap(string user, string pass)
    {
        string sql = "select TenDn from KhachHang where TenDn=@user and MatKhau=@pass";
        SqlParameter u = new SqlParameter("@user", user);
        SqlParameter p = new SqlParameter("@pass", pass);
        DataTable dt = data.GetData(sql, u, p);
        if (dt.Rows.Count > 0)
            return dt.Rows[0][0].ToString();
        else
            return "";

    }
    public DataTable SuaHoiDap(string nguoihoi, string email, string chude, string hoi, string dap, string  ngayhoi, string ngaydap, string nguoidap, bool hienthi)
    {
        SqlParameter pnguoihoi = new SqlParameter("@nguoihoi", nguoihoi);
        SqlParameter pEmail = new SqlParameter("@email", email);
        SqlParameter pchude = new SqlParameter("@chude", chude);
        SqlParameter phoi = new SqlParameter("@hoi", hoi);
        SqlParameter pdap = new SqlParameter("@dap", dap);
        SqlParameter pngayhoi = new SqlParameter("@ngayhoi", ngayhoi);
        SqlParameter pngaydap = new SqlParameter("@ngaydap", ngaydap);
        SqlParameter pnguoidap = new SqlParameter("@nguoidap", nguoidap);
        SqlParameter phienthi = new SqlParameter("@hienthi", hienthi);
        string sql = "update HoiDap set Email=@email,ChuDe=@chude,Hoi=@hoi,Dap=@dap,NgayHoi=@ngayhoi,NgayDap=@ngaydap,NguoiDap=@Nguoidap,HienThi=@hienthi where NguoiHoi=@nguoihoi";
        return data.GetData(sql, pnguoihoi, pEmail, pchude, phoi, pdap, pngayhoi, pngaydap, pnguoidap, phienthi);
    }
    public DataTable XoaHoiDap(string nguoihoi)
    {
        string xoa = "delete from HoiDap where NguoiHoi=@nguoihoi";
        SqlParameter pxoa = new SqlParameter("@nguoihoi", nguoihoi);
        return data.GetData(xoa, pxoa);
    }
}
